package xzy.com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import xzy.com.config.jwt.annotation.PassToken;
import xzy.com.entity.domain.Notice;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.NoticeService;
/**
 * 
* @Title: NoticeController
* @Description: 新闻公告控制层
* @author: liyang
* @date 2020年12月6日 下午4:50:47
 */
@RestController
@PassToken
@RequestMapping("/api/notice")
public class NoticeController {
	@Autowired
	private NoticeService noticeService;
	
	@PassToken
	@PostMapping("/save")
	public ResponseBean add(@RequestBody Notice notice) {
		if (notice != null) {
			Boolean result = noticeService.save(notice);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "添加成功", true);
			}else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "添加数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "数据不能为空", null);
	}

	@PassToken
	@DeleteMapping("/delete/{id}")
	public ResponseBean delete(@PathVariable("id") Long id) {
		if(null!=null) {
			boolean isDel = noticeService.removeById(id);
			if (isDel) {
				return new ResponseBean(HttpStatus.OK.value(), "删除数据成功!", isDel);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 分页处理
	@PassToken
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Notice> page = new Page<>(pages, size);
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("id");
		noticeService.page(page, queryWrapper);
		Page<Notice> pages1 = noticeService.page(page, queryWrapper);
		
		Map<String,Object> map = new HashMap<>();
		map.put("list", pages1.getRecords());
		map.put("recordTotal", pages1.getTotal());
		map.put("pageTotal", pages1.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 搜索查询分页处理
	@PassToken
	@GetMapping("/search")
	public ResponseBean  searchListPages(@RequestParam("title") String title,@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Notice> page = new Page<>(pages, size);
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("id");
		if(null!=title) {
			queryWrapper.like("title", title);
		}
		Page<Notice> pages1 = noticeService.page(page, queryWrapper);
        //IPage<Menu> menuPage = menuService.findParentMenu(pages1, queryWrapper);
		//查询父级菜单
				
		Map<String,Object> map = new HashMap<>();
		map.put("list", pages1.getRecords());
		map.put("recordTotal", pages1.getTotal());
		map.put("pageTotal", pages1.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}
	
	//修改
	@PassToken
	@PutMapping("/update")
	public ResponseBean updateUser(@RequestBody Notice notice) {
		if (null!=notice) {// 核验数据
			boolean result = noticeService.updateById(notice);
			if(result) {
				new ResponseBean(HttpStatus.OK.value(), "修改成功", true);
			}else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
	}
	
	@PassToken
	@DeleteMapping("/delete")
	public ResponseBean delete(@RequestParam("id") Integer id) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
		if (null!=id) {
			boolean result = noticeService.removeById(id);
			if(result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			}else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	//传入的json数组key=list，为一组整数索引
	@PassToken
	@DeleteMapping("/deleteAll")
	public ResponseBean deleteAll(@RequestBody JSONObject jsonObject) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "批量删除失败", null);
		JSONArray jsonArray = jsonObject.getJSONArray("list");
		List<Integer> idList = jsonArray.toJavaList(Integer.class);
		if (!idList.isEmpty()) {
			boolean result = noticeService.removeByIds(idList);
			if(result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			}else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}
	
	@PassToken
	@GetMapping("/findById")
	public ResponseBean findById(@RequestParam("id")Long id) {
		if (id != null) {
			Notice notice = noticeService.getById(id);
			if (null!=notice) {
				return new ResponseBean(HttpStatus.OK.value(), "查询成功", notice);
			}else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}
}
